<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final
 * @category    Controllers
 * @see 		Products
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Products extends CI_Controller {
	var $cat = '';
	var $q = '';
	var $copyid = '';
	var $limit = '';
	var $orderby = '';
	var $ordermode = '';
	var $updated = '';
	var $failed = '';
	var $error = '';
	var $name = '';
	var $uri_arr = array();
	var $uri_str = '';
	var $site_url = '';

	public function __construct()
	{
		parent::__construct();
		$this->load->model('hiti_model', 'hiti', true);
		$this->hiti->load_config();
		if (check_rights() == false)
		{
			redirect();
		}
		$this->load->model($GLOBALS['var']['act'].'_model', 'Pro');
		$this->cat = $GLOBALS['var']['filter_cat'];
		$this->q = $this->input->get('q', true);
		$this->copyid = $this->input->get('copyid', true);
		$this->orderby = $this->input->get('orderby', true);
		$this->ordermode = $this->input->get('ordermode', true);
		$this->updated = $this->input->get('updated', true);
		$this->failed = $this->input->get('failed', true);
		$this->error = $this->input->get('error', true);
		$this->name = $this->input->get('name', true);
		$this->uri_arr = array(
			'deleted' => $GLOBALS['var']['deleted'],
			'cat' => $this->cat,
			'q' => $this->q,
			'copyid' => $this->copyid,
			'limit' => $this->limit,
			'rowstart' => $GLOBALS['var']['rowstart'],
			'orderby' => $this->orderby,
			'ordermode' => $this->ordermode
		);
		$this->uri_str = url_uri($this->uri_arr);
		$this->site_url = site_url($GLOBALS['var']['act']);
		$this->image_w = $GLOBALS['cfg']['prod_image_width'];
		$this->image_h = $GLOBALS['cfg']['prod_image_height'];
		$this->thumb_w = $GLOBALS['cfg']['prod_thumb_width'];
		$this->thumb_h = $GLOBALS['cfg']['prod_thumb_height'];
		$this->gallery_w = $GLOBALS['cfg']['prod_gallery_width'];
		$this->gallery_h = $GLOBALS['cfg']['prod_gallery_height'];
	}

	public function index()
	{
		/*
		* Huy tham so copy trong tham so url
		*/
		$this->uri_arr['copyid'] = '';
		/*
		* Xu ly du lieu
		*/
		$num_rows = $this->Pro->show($this->uri_arr, true);
		$page_list = page_list($num_rows, $this->uri_arr);
		$cat_list = $this->Pro->filter_category_list();
		$data = array(
			'orderby' => $this->orderby,
			'ordermode' => $this->ordermode,
			'updated' => $this->updated,
			'failed' => $this->failed,
			'name' => $this->name,
			'uri_str' => $this->uri_str,
			'products' => array(),
			'page_list' => $page_list,
			'cat_list' => $cat_list,
			'filter_cat' => $GLOBALS['var']['filter_cat'],
			'filter_category_list' => $cat_list,
			'page_list' => page_list($num_rows, $this->uri_arr)
		);
		$rows = $this->Pro->show($this->uri_arr);
		if ($rows) $data['products'] = $rows;
		/*
		* Hien thi
		*/
		$header = array(
			'title' => 'Danh sách sản phẩm',
			'add_link' => current_url().'/update',
			'search' => true,
			'page_list' => $page_list,
			'datetime_picker' => false,
			'submit_btn' => false,
			'cat_list' => $cat_list,
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/index', $data);
		$this->load->view('footer');
	}

	public function update($id = '')
	{
		/*
		* Kiem tra quyen cap nhap
		*/
		if ((!$id && !$GLOBALS['per']['add']) || ($id && !$GLOBALS['per']['edit']))
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Xu ly du lieu
		*/
		if ($this->copyid && !$id)
		{
			$info = $this->Pro->info($this->copyid);
			$info['img'] = '';
			$info['img_slider'] = '';
			$info['images'] = '';
			$info['code'] = randomkeys(8);
		}
		else
		{
			$info = $this->Pro->info($id);
		}
		if (!$id) $info['code'] = randomkeys(8);
		$data = array(
			'info' => $info,
			'updated' => $this->updated,
			'failed' => $this->failed,
			'name' => $this->name,
			'error' => $this->error,
			'action' => site_url($GLOBALS['var']['act'].'/process').$this->uri_str,
			'image_width' => $this->image_w,
			'image_height' => $this->image_h,
			'thumb_width' => $this->thumb_w,
			'thumb_height' => $this->thumb_h,
			'gallery_width' => $this->gallery_w,
			'gallery_height' => $this->gallery_h,
			'category_list' => $this->Pro->category_list(),
			'trade_list' => $this->Pro->trade_list(),
			'sanphamdenghi_list' => $this->Pro->opt_product_list($info['sanphamdenghi']),
			'sanphammaukhac_list' => $this->Pro->opt_product_list($info['sanphammaukhac']),
			'xuatxu_list' => $this->Pro->properties_data('xuatxu'),
			'chatlieu_list' => $this->Pro->properties_data('chatlieu'),
			'bh_list' => $this->Pro->properties_data('bh'),
			'dvt_list' => $this->Pro->properties_data('dvt'),
			'size_list' => $this->Pro->properties_data('size'),
			'color_list' => $this->Pro->properties_data('color'),
			'tinhtrang_list' => $this->Pro->properties_data('tinhtrang'),
			'article_categories' => $this->Pro->article_categories(),
			'checked_cat' => $this->input->cookie('cat', true),
			'checked_cat2' => $this->input->cookie('cat2', true)
		);
		/*
		* Hien thi
		*/
		$header = array(
			'title' => ($id ? 'Cập nhập' : 'Thêm mới').' sản phẩm',
			'add_link' => '',
			'search' => false,
			'page_list' => '',
			'datetime_picker' => false,
			'submit_btn' => true,
			'cat_list' => array(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/update', $data);
		$this->load->view('footer');
	}

	public function process()
	{
		/*
		* Kiem tra POST method
		*/
		if (!$_POST)
		{
			redirect();
		}
		$name_vn = $this->input->post('name_vn', true);
		/*
		* Them tham so url
		*/
		if ($this->copyid)
		{
			$id = '';
		}
		else
		{
			$id = $this->input->post('id', true);
		}
		if ((!$id && !$GLOBALS['per']['add']) || ($id && !$GLOBALS['per']['edit']))
		{
			redirect($GLOBALS['var']['act']);
		}
		$this->uri_arr['name'] = str_replace('&', '', $name_vn);
		/*
		* Kiem tra token va tham so yeu cau
		*/
		if (!token_validation() || $name_vn == '')
		{
			redirect($GLOBALS['var']['act'].'/update'.($id ? '/'.$id : '').url_uri($this->uri_arr));
		}
		/*
		* Xu ly du lieu
		*/
		$name_en = $this->input->post('name_en', true);
		if($name_en == '') $name_en = $name_vn;
		$alt_vn = $this->input->post('alt_vn', true);
		$alt_en = $this->input->post('alt_en', true);
		if($alt_en == '') $alt_en = $alt_vn;
		$cat = $this->input->post('cat', true);
		$data = array(
			'name_vn' => $name_vn,
			'name_en' => $name_en,
			'des_vn' => $this->input->post('des_vn'),
			'des_en' => $this->input->post('des_en'),
			'tacgia' => $this->input->post('tacgia', true),
			'sotrang' => $this->input->post('sotrang', true),
			'alt_vn' => $alt_vn,
			'alt_en' => $alt_en,
	        'color' => json_encode($this->input->post('color', true)),
	        'hide_color' => $this->input->post('hide_color', true),
			'size' => json_encode($this->input->post('size', true)),
	        'hide_size' => $this->input->post('hide_size', true),
	        'kichthuoc' => $this->input->post('kichthuoc', true),
	        'hide_kichthuoc' => $this->input->post('hide_kichthuoc', true),
	        'chatlieu' => json_encode($this->input->post('chatlieu', true)),
	        'hide_chatlieu' => $this->input->post('hide_chatlieu', true),
	        'cat' => (is_array($cat) ? json_encode($cat) : $cat),
	        'keyword' => url_title(viet_decode($name_vn), '-', true),
	        'info_vn' => $this->input->post('info_vn'),
			'info_en' => $this->input->post('info_en'),
			'con_vn' => $this->input->post('con_vn'),
			'con_en' => $this->input->post('con_en'),
	        'nsx' => $this->input->post('nsx', true),
	        'hide_nsx' => $this->input->post('hide_nsx', true),
	        'tech' => $this->input->post('tech', true),
	        'hide_tech' => $this->input->post('hide_tech', true),
			'ncc' => $this->input->post('ncc', true),
			'nganh' => $this->input->post('nganhhang', true),
	        'gia1' => str_replace(',', '', $this->input->post('gia1', true)),
			'gia2' => str_replace(',', '', $this->input->post('gia2', true)),
			'khuyenmai' => $this->input->post('khuyenmai', true),
			'giam' => $this->input->post('giam', true),
			'xuatxu' => $this->input->post('xuatxu', true),
	        'hide_xuatxu' => $this->input->post('hide_xuatxu', true),
	        'dvt' => $this->input->post('dvt', true),
			'bh' => $this->input->post('bh', true),
	        'hide_bh' => $this->input->post('hide_bh', true),
	        'tags' => $this->input->post('tags', true),
	        'hide_tags' => $this->input->post('hide_tags', true),
	        'seo_desc' => $this->input->post('seo_desc', true),
			'code' => $this->input->post('code', true),
	        'hide_code' => $this->input->post('hide_code', true),
	        'active' => $this->input->post('active', true),
	        'comment' => $this->input->post('comment', true),
	        'sitemap' => $this->input->post('sitemap', true),
	        'new' => $this->input->post('new', true),
	        'best' => $this->input->post('best', true),
	        'hot' => $this->input->post('hot', true),
	        'sanphamdenghi' => json_encode($this->input->post('sanphamdenghi', true)),
	        'sanphammaukhac' => json_encode($this->input->post('sanphammaukhac', true)),
	        'pricelist' => json_encode($this->input->post('pricelist', true)),
	        'tinhtrang' => $this->input->post('tinhtrang', true),
	        'hide_tinhtrang' => $this->input->post('hide_tinhtrang', true),
	        'img_alt' => $this->input->post('img_alt', true),
	        'img_slider_alt' => $this->input->post('img_slider_alt', true),
	        'images' => json_encode($this->input->post('images', true)),
	        'articles' => json_encode($this->input->post('element', true)),

            'doituong' => $this->input->post('doituong', true),
            'congdung' => $this->input->post('congdung', true),
            'kichthuoc' => $this->input->post('kichthuoc', true),

	        'date_added' => date(TIME_SQL),
            'date_modified' => date(TIME_SQL)
		);
		/*
		* Upload file
		*/
		$this->load->library('upload');
		$upload_data = array();
		foreach ($_FILES as $field => $value)
		{
		    if (!empty($value['name']))
		    {
		        $upload_data[$field] = upload($field, url_title(viet_decode($name_vn), '-', true), PRO);
		        $file_name = $upload_data[$field]['file_name'];
		        $file_path = $upload_data[$field]['file_path'];
		        $full_path = $upload_data[$field]['full_path'];
		        if ($file_name)
		        {
		        	$data[$field] = $file_name;
		        	$siglogo_color = $this->input->post('siglogo_color', true);
		        	$siglogo_gray = $this->input->post('siglogo_gray', true);
		        	if($siglogo_color) sig_logo($full_path, $full_path, 1, $siglogo_color);
                    if($siglogo_gray) sig_logo($full_path, $full_path, 2, $siglogo_gray);
		        	make_thumb($full_path, $file_path.'thumbs/'.$file_name, $this->thumb_w, $this->thumb_h);
		        }
		    }
		}
		/*
		* Ghi du lieu
		*/
		if ($this->Pro->process($data, $id))
		{
			$this->uri_arr['updated'] = 1;
		}
		else
		{
			$this->uri_arr['failed'] = 1;
		}
		/*
		* Chuyen huong
		*/
		$this->uri_arr['copyid'] = '';
		if ($id > 0)
		{
			redirect($GLOBALS['var']['act'].'/update/'.$id.url_uri($this->uri_arr));
		}
		else
		{
			redirect($GLOBALS['var']['act'].url_uri($this->uri_arr));
		}
	}

	public function quick_edit()
	{
		global $orderStatus;
		$id = $this->input->post('id', true);
		if (!$id)
		{
			echo 0;
			return false;
		}
		$info = $this->Pro->info($id);
		$product_status = array();
		foreach ($this->Pro->properties_data('tinhtrang') as $row)
		{
			$product_status[$row['id']] = $row['name_vn'];
		}
		$data = array(
			'info' => $info,
			'category_list' => $this->Pro->category_list(),
			'product_status' => $product_status
		);
		$this->load->view($GLOBALS['var']['act'].'/quick_edit', $data);
	}

	public function quick_save()
	{
		$id = $this->input->post('id', true);
		$name_vn = $this->input->post('name_vn', true);
		if (!$id || !$name_vn)
		{
			echo 0;
			return false;
		}
		$name_en = $this->input->post('name_en', true);
		if ($name_en == '')
		{
			$name_en = $name_vn;
		}
		$cat = $this->input->post('cat', true);
		$data = array(
			'name_vn' => $name_vn,
	    	'name_en' => $name_en,
	        'keyword' => url_title(viet_decode($name_vn), '-', true),
	        'cat' => (is_array($cat) ? json_encode($cat) : $cat),
	        'comment' => $this->input->post('comment', true),
	        'sitemap' => $this->input->post('sitemap', true),
	        'khohang' => $this->input->post('khohang', true),
	        'hide_code' => $this->input->post('hide_code', true),
	        'hide_nsx' => $this->input->post('hide_nsx', true),
	        'hide_size' => $this->input->post('hide_size', true),
	        'hide_kichthuoc' => $this->input->post('hide_kichthuoc', true),
	        'hide_color' => $this->input->post('hide_color', true),
	        'hide_chatlieu' => $this->input->post('hide_chatlieu', true),
	        'hide_bh' => $this->input->post('hide_bh', true),
	        'hide_xuatxu' => $this->input->post('hide_xuatxu', true),
	        'hide_tags' => $this->input->post('hide_tags', true),
	        'tinhtrang' => $this->input->post('tinhtrang', true),
	        'hide_tinhtrang' => $this->input->post('hide_tinhtrang', true),
	        'date_modified' => date(TIME_SQL)
		);
		if ($this->Pro->quick_save($data, $id))
		{
			echo 1;
		}
		else
		{
			echo 0;
		}
		return false;
	}

	public function list_product()
	{
		$show = $this->input->post('show', true);
		if (!$show)
		{
			return false;
		}
		/*
		* Xu ly du lieu
		*/
		$this->rowstart = $this->input->post('rowstart', true);
		$this->cat = $this->input->post('cat', true);
		$this->uri_arr['cat'] = $this->cat;
		$this->uri_arr['rowstart'] = $this->rowstart;
		$this->uri_arr['q'] = $this->input->post('q', true);
		$proids = $this->input->post('proids', true);
		$this->uri_arr['proids'] = $proids;
		$rows = $this->Pro->list_product($this->uri_arr);
		$num_rows = $this->Pro->num_product($this->uri_arr);
		$data = array(
			'rowstart' => $this->rowstart,
			'products' => array(),
			'proids' => explode(',', $proids),
			'page_list' => page_list($num_rows, $this->uri_arr, 5, true)
		);
		if ($rows) $data['products'] = $rows;
		/*
		* Hien thi
		*/
		$this->load->view($GLOBALS['var']['act'].'/list_product', $data);
	}

	public function list_article()
	{
		$show = $this->input->post('show', true);
		if (!$show)
		{
			//return false;
		}
		/*
		* Xu ly du lieu
		*/
		$this->rowstart = $this->input->post('rowstart', true);
		$this->cat = $this->input->post('cat', true);
		$this->uri_arr['cat'] = $this->cat;
		$this->uri_arr['rowstart'] = $this->rowstart;
		$this->uri_arr['q'] = $this->input->post('q', true);
		$newsids = $this->input->post('newsids', true);
		$articles_added = $this->input->post('articles_added', true);
		$this->uri_arr['newsids'] = $newsids;
		$this->uri_arr['articles_added'] = $articles_added;
		$rows = $this->Pro->list_article($this->uri_arr);
		$num_rows = $this->Pro->num_article($this->uri_arr);
		$data = array(
			'rowstart' => $this->rowstart,
			'articles' => array(),
			'articles_added' => explode(',', $articles_added),
			'newsids' => explode(',', $newsids),
			'page_list' => page_list($num_rows, $this->uri_arr, 5, true)
		);
		if ($rows) $data['articles'] = $rows;
		/*
		* Hien thi
		*/
		$this->load->view($GLOBALS['var']['act'].'/list_article', $data);
	}
}

/* End of file products.php */
/* Location: ./application/controllers/products.php */